package com.liwei.controller;
import com.liwei.beans.Stu;
import com.liwei.dto.ResultDTO;
import com.liwei.exception.MyDateExceptionAdvice;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;

@RestController
@RequestMapping("/")
public class IndexController {
    @RequestMapping("get.action")
    public ResultDTO get(Stu stu, BindingResult bindingResult) throws MyDateExceptionAdvice {
        if (bindingResult.hasErrors()){
            throw new MyDateExceptionAdvice("生日输入错误");
        }
        System.out.println(bindingResult.getFieldValue("age"));
        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(resultDTO.SUCCESS);
//        HashMap<String,String> hashMap=new HashMap<>();
//        hashMap.put("name",stu.getName());
//        LocalDate age=stu.getAge();
//        String strAge=age.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
//        hashMap.put("age",strAge);
//        resultDTO.setData(hashMap);

        HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("name",stu.getName());
        hashMap.put("age",bindingResult.getFieldValue("age").toString());
        resultDTO.setData(hashMap);
        return resultDTO;
    }
}
